﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;//序列化和反序列化
using System.IO;
namespace _84XML保存对象
{
    internal class Program
    {
        //测试保存
        private static void Main(string[] args)
        {
            Student s1 = new Student() { name = "张三", age = 20, height = 1.7f };
            Student s2 = new Student() { name = "李四", age = 22, height = 1.5f };

            Student[] array = { s1, s2 };

            Student.SaveXmlFile(array, @"D:\Students.xml");
        }

        //测试读取
        private static void Main2(string[] args)
        {
            Student[] students = Student.LoadFromXmlFile(@"D:\Students.xml");
        }

        //其实这个功能不用我们自己写,有现成的XmlSerializer类可以帮我们完成
        private static void Main3(string[] args)
        {
            //保存
            //Student s1 = new Student() { name = "张三", age = 20, height = 1.7f };
            //Student s2 = new Student() { name = "李四", age = 22, height = 1.5f };

            //Student[] array = { s1, s2 };

            // XmlSerializer serializer = new XmlSerializer(array.GetType());
            // string path = @"D:\Student自动序列化.xml";
            // FileStream stream = File.OpenWrite(path);
            // serializer.Serialize(stream, array);

            //还原
            XmlSerializer serializer = new XmlSerializer(typeof(Student[]));
            string path = @"D:\Student自动序列化.xml";
            FileStream stream = File.OpenRead(path);
            Student[] students = serializer.Deserialize(stream) as Student[];
        }
    }

    [Serializable]
    public class Student
    {
        public string name;
        public int age;
        public float height;

        public static void SaveXmlFile(Student[] students, string path)
        {
            //创建一个xml对象
            XmlDocument xml = new XmlDocument();
            //创建根节点
            XmlElement root = xml.CreateElement("Students");
            //添加根节点
            xml.AppendChild(root);

            //遍历每个学生
            foreach (var stu in students)
            {
                XmlElement elementStu = xml.CreateElement("Student");//为当前学生创建一个节点
                //给这个节点设置属性
                elementStu.SetAttribute("name", stu.name);
                elementStu.SetAttribute("age", stu.age.ToString());
                elementStu.SetAttribute("height", stu.height.ToString());
                //把这个学生节点添加到根节点下面
                root.AppendChild(elementStu);
            }
            //保存
            xml.Save(path);
        }

        public static Student[] LoadFromXmlFile(string path)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(path);

            XmlNode root = xml.SelectSingleNode("/Students");
            XmlNodeList list = root.ChildNodes;
            Student[] students = new Student[list.Count];
            for (int i = 0; i < list.Count; i++)
            {
                XmlNode node = list[i];
                students[i] = new Student()
                {
                    name = node.Attributes["name"].Value,
                    age = int.Parse(node.Attributes["age"].Value),
                    height = float.Parse(node.Attributes["height"].Value),
                };
            }

            return students;
        }
    }
}